package com.hassean.we.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hassean.we.admin.common.constant.CommonConstant;
import com.hassean.we.admin.common.util.Query;
import com.hassean.we.admin.common.util.R;
import com.hassean.we.admin.model.entity.TbLog;
import com.hassean.we.admin.service.TbLogService;
import com.hassean.we.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author yajun
 * @since 2019-04-25
 */
@Api(description = "日志接口")
@RestController
@RequestMapping("/tbLog")
public class TbLogController extends BaseController {
    @Autowired private TbLogService tbLogService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return TbLog
    */
    @ApiOperation(value = "查询日志" ,  notes="查询日志")
    @GetMapping("/{id}")
    public R<TbLog> get(@PathVariable Integer id) {
        return new R<>(tbLogService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @ApiOperation(value = "分页查询日志" ,  notes="分页查询日志")
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        EntityWrapper entity = new EntityWrapper<>();

        entity.orderDesc(Arrays.asList(new String [] {"id"}));
        if (params.get("page")!=null&&!(params.get("page")).equals("")){
            Integer page = Integer.parseInt(params.get("page").toString());
            params.put("page",page);
        }else{
            Integer page = 1;
            params.put("page",page);
        }

        if (params.get("limit")!=null&&!(params.get("limit")).equals("")){
            Integer limit = Integer.parseInt(params.get("limit").toString());
            System.out.println("limit:"+limit);
        }else{
            Integer limit = 10;
            params.put("limit",limit);
        }
        return tbLogService.selectPage(new Query<>(params),entity );
    }

    /**
     * 添加
     * @param  tbLog  实体
     * @return success/false
     */
    @ApiOperation(value = "添加日志" ,  notes="添加日志")
    @PostMapping
    public R<Boolean> add(@RequestBody TbLog tbLog) {
        return new R<>(tbLogService.insert(tbLog));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @ApiOperation(value = "删除日志" ,  notes="删除日志")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        TbLog tbLog = new TbLog();
        tbLog.setId(id);
        tbLog.setUpdateTime(new Date());
        tbLog.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(tbLogService.updateById(tbLog));
    }

    /**
     * 编辑
     * @param  tbLog  实体
     * @return success/false
     */
    @ApiOperation(value = "编辑日志" ,  notes="编辑日志")
    @PutMapping
    public R<Boolean> edit(@RequestBody TbLog tbLog) {
        tbLog.setUpdateTime(new Date());
        return new R<>(tbLogService.updateById(tbLog));
    }
    @ApiOperation(value = "测试空指针异常" ,  notes="测试空指针异常")
    @RequestMapping("/ex")
    public String ex() {

        int i = 1;
        Integer a =null;
        int c =i+a;
        System.out.println("空指针异常:"+c);
        return "我是错误信息";
    }
    @ApiOperation(value = "测试异常" ,  notes="测试异常")
    @RequestMapping("/ex2")
    public String ex2() {

        int i = 1/0;


        return "我是错误信息";
    }
}
